<!-- components/TaskCard.vue -->
<script setup>
import { useTaskStore } from '@/stores/taskStore'
import { useUserStore } from '@/stores/userStore'

const props = defineProps(['task'])
const store = useTaskStore()
const userStore = useUserStore()

const assignToMe = () => {
  if (!userStore.currentUser) {
    alert('请先登录')
    return
  }

  // 找到当前登录用户对应的成员
  const member = store.members.find(m => m.name === userStore.currentUser)
  
  if (!member) {
    alert('找不到对应的成员信息')
    return
  }

  if (member.currentLoad >= member.capacity) {
    alert('您当前的工作量已满，无法分配更多任务')
    return
  }

  // 分配任务给当前用户
  const task = store.tasks.find(t => t.id === props.task.id)
  if (task) {
    task.assignedTo = member.id
    member.currentLoad += 1
    
    // 如果任务在待办状态，自动转为进行中
    if (task.status === 'todo') {
      store.updateTaskStatus(task.id, 'doing')
    }
  }
}
</script>

<template>
  <div class="task-card" :class="`priority-${task.priority}`">
    <div class="task-header">
      <h4>{{ task.title }}</h4>
      <span class="priority-badge">{{ 
        task.priority === 'high' ? '高' : 
        task.priority === 'medium' ? '中' : '低' 
      }}</span>
    </div>
    <p class="task-description">{{ task.description }}</p>
    <div class="task-footer">
      <span class="date">{{ new Date(task.createdAt).toLocaleDateString() }}</span>
      <button 
        v-if="!task.assignedTo" 
        @click="assignToMe"
        class="assign-btn"
      >
        分配给我
      </button>
      <div v-else class="assigned-to">
        <span class="label">分配给:</span>
        <span class="member">{{ store.members.find(m => m.id === task.assignedTo)?.name }}</span>
      </div>
    </div>
  </div>
</template>

<style scoped>
.task-card {
  background: white;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  border-left: 4px solid #e9ecef;
  transition: transform 0.2s, box-shadow 0.2s;
}

.task-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.task-card.priority-high {
  border-left-color: #ff6b6b;
}

.task-card.priority-medium {
  border-left-color: #feca57;
}

.task-card.priority-low {
  border-left-color: #1dd1a1;
}

.task-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.task-header h4 {
  margin: 0;
  font-size: 1rem;
  color: #2c3e50;
  font-weight: 600;
}

.priority-badge {
  font-size: 0.7rem;
  padding: 3px 8px;
  border-radius: 10px;
  font-weight: 500;
}

.task-card.priority-high .priority-badge {
  background: #ff6b6b;
  color: white;
}

.task-card.priority-medium .priority-badge {
  background: #feca57;
  color: #2c3e50;
}

.task-card.priority-low .priority-badge {
  background: #1dd1a1;
  color: white;
}

.task-description {
  margin: 0 0 12px 0;
  color: #495057;
  font-size: 0.9rem;
  line-height: 1.5;
}

.task-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  color: #6c757d;
}

.date {
  font-size: 0.75rem;
}

.assign-btn {
  background: #42b983;
  color: white;
  border: none;
  padding: 5px 12px;
  border-radius: 4px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.2s;
}

.assign-btn:hover {
  background: #3aa876;
}

.assigned-to {
  display: flex;
  align-items: center;
  gap: 5px;
}

.assigned-to .label {
  color: #6c757d;
}

.assigned-to .member {
  font-weight: 500;
  color: #2c3e50;
}
</style>